package com.example.city_home_app.RecyclerViewAdapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.city_home_app.Activity.PhotoViewActivity;
import com.example.city_home_app.Bean.Down__Image;
import com.example.city_home_app.Bean.PostContent;
import com.example.city_home_app.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Adapter_PostContent extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<PostContent> list;

    private ArrayList<String> imgurl;

    public Adapter_PostContent(Context context) {
        this.context = context;
    }

    public void setList(List<PostContent> list) {
        this.list = list;
        imgurl = new ArrayList<>();
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.recitem_post_content, parent, false);
        Adapter_PostContent.ViewHolder viewHolder = new Adapter_PostContent.ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final PostContent bean = list.get(position);
        if(bean == null){
            return;
        }
        final Adapter_PostContent.ViewHolder viewHolder = (Adapter_PostContent.ViewHolder) holder;
        if(bean.getType()==1){
            viewHolder.imgRecitemPostcontent.setVisibility(View.GONE);
            viewHolder.tvRecitemPostcontent.setText(bean.getContent());
        }else{
            viewHolder.tvRecitemPostcontent.setVisibility(View.GONE);
            Picasso.with(context)
                    .load("http://siminzhang.top:8088/city_home_server/public/static/posters/"+bean.getContent())
                    .into(viewHolder.imgRecitemPostcontent);
            imgurl.add(bean.getContent());
        }

        viewHolder.imgRecitemPostcontent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(bean.getType()!=1){
                    Intent intent = new Intent(context, PhotoViewActivity.class);
                    intent.putStringArrayListExtra("urls",imgurl);
                    intent.putExtra("currentPosition", 0);
                    context.startActivity(intent);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_recitem_postcontent)
        TextView tvRecitemPostcontent;
        @BindView(R.id.img_recitem_postcontent)
        ImageView imgRecitemPostcontent;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
